﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;

namespace MMS
{
    public partial class Frm_Sign : UIForm
    {
        public Frm_Sign()
        {
            InitializeComponent();
        }
        db_mmsEntities conn = new db_mmsEntities();
        private void Frm_Sign_Load(object sender, EventArgs e)
        {
            var name = conn.tb_root.GroupBy(r => r.storeName).Select(k => k.Key).ToList();
            select_Store.DataSource = name;
        }
        /// <summary>
        /// 员工注册
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Staff_Click(object sender, EventArgs e)
        {
            string nb = txt_NbStaff.Text;
            string pw = txt_PwStaff.Text;
            string store = select_Store.Text;
            if (nb.Length != 8)
            {
                MessageBox.Show("你输入工号有误！");
                return;
            }
            else
            {
                var temp = conn.tb_staff.FirstOrDefault(r => r.staffID == nb);
                if (temp != null)
                {
                    MessageBox.Show("该工号已被注册！");
                    return;
                }
            }
            if (pw.Length < 6 || pw.Length > 10)
            {
                MessageBox.Show("你输入密码长度有误！");
                return;
            }
            if (store == "")
            {
                MessageBox.Show("请选择工作单位！");
                return;
            }
            tb_staff staff = new tb_staff { staffID=nb, password=pw, storeName =store, isUse=0 };
            //填充数据
            conn.tb_staff.Add(staff);
            if (conn.SaveChanges() ==1)
            {
                MessageBox.Show("注册成功！");
                this.Close();
            }
            else
            {
                MessageBox.Show("注册失败！");
            }

        }
        /// <summary>
        /// 管理员注册
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Root_Click(object sender, EventArgs e)
        {
            string nb = txt_NbRoot.Text;
            string pw = txt_PwRoot.Text;
            string store = txt_Store.Text;
            if (nb.Length != 6)
            {
                MessageBox.Show("你输入账户有误！");
                return;
            }
            else
            {
                var temp = conn.tb_root.FirstOrDefault(r => r.rootID==nb);
                if (temp != null)
                {
                    MessageBox.Show("该账户已被注册！");
                    return;
                }
            }
            if (pw.Length < 6 || pw.Length > 10)
            {
                MessageBox.Show("你输入密码长度有误！");
                return;
            }
            if (store == "")
            {
                MessageBox.Show("请输入超市名称");
                return;
            }
            tb_root root = new tb_root { rootID = nb, password = pw, storeName = store };
            conn.tb_root.Add(root);
            if (conn.SaveChanges() == 1)
            {
                MessageBox.Show("注册成功！");
                this.Close();
            }
            else
            {
                MessageBox.Show("注册失败！");
            }
        }
    }
}
